#include  "exit.h"

bool exit_flag;

void EXTI0_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

/**
 * 外部中断初始化函数
 * 中断源位PB0，初始化为下降沿触发，优先级1，子优先级0，使能中断.
 * 用于检测APDS9960的INT引脚的低电平触发中断
 */
void exti0_int_init(void)
{
    GPIO_InitTypeDef     GPIO_InitStructure = {0};
    EXTI_InitTypeDef     EXTI_InitStructure = {0};
    NVIC_InitTypeDef     NVIC_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    GPIO_SetBits(GPIOB,GPIO_Pin_0);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0);
    EXTI_InitStructure.EXTI_Line = EXTI_Line0;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

void EXTI0_IRQHandler(void)
{
    Delay_Ms(5);//消抖    //(要消抖，否则过一段时间就卡死 ,消抖短一点，准确率更高)
    if(EXTI_GetITStatus(EXTI_Line0) != RESET)
    {
        exit_flag = 1;
        printf("Run at EXTI\r\n");
        EXTI_ClearITPendingBit(EXTI_Line0); /* Clear Flag */
    }
}
